﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DeliveryPlus.Web.Controllers;
using DeliveryPlus.Web.DAL.Entities;

namespace DeliveryPlus.Web.Areas.ControlPanel.Controllers
{
    public class ManageAboutDeliveryController : BaseController
    {
        //
        // GET: /ControlPanel/ManageAboutDelivery/

        /// <summary>
        /// Отображает список новостей отсортированный по дате публикации
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var about = DataContext.Articles.Where(i => i.IsAboutDelivery == true).ToList();
            return View(about);
        }

        /// <summary>
        /// Отображает форму добавления новой публикации
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View("EditAboutDelivery", new Article() { PublicationDate = DateTime.Now });
        }

        /// <summary>
        /// Отображает форму редактирования указанной новостной публикации
        /// </summary>
        /// <param name="id">Идентификатор новости</param>
        /// <returns></returns>
        public ActionResult Edit(long id)
        {
            var article = DataContext.Articles.FirstOrDefault(a => a.Id == id);
            if (article == null)
            {
                return RedirectToAction("Index");
            }

            return View("EditAboutDelivery", article);
        }

        /// <summary>
        /// Удаляет указанную новостную публикацию
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(long id)
        {
            var article = DataContext.Articles.FirstOrDefault(a => a.Id == id);
            if (article == null)
            {
                return RedirectToAction("Index");
            }

            DataContext.Articles.DeleteOnSubmit(article);
            DataContext.SubmitChanges();

            return RedirectToAction("Index");
        }

        /// <summary>
        /// Обрабатывает создание или сохранение новостной публикации
        /// </summary>
        /// <param name="model">Модель</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public ActionResult Save(Article model)
        {
            if (model.Id <= 0)
            {
                model.DateCreated = DateTime.Now;
                model.IsAboutDelivery = true;
                DataContext.Articles.InsertOnSubmit(model);
                DataContext.SubmitChanges();
            }
            else
            {
                var article = DataContext.Articles.FirstOrDefault(a => a.Id == model.Id);
                if (article == null)
                {
                    return RedirectToAction("Index");
                }

                article.Title = string.Empty;
                article.PublicationDate = DateTime.Now;
                article.Annotation = string.Empty;
                article.Content = model.Content;
                article.DateModified = model.DateModified;
                article.IsAboutDelivery = true;
                DataContext.SubmitChanges();
            }

            return RedirectToAction("Index");
        }

    }
}
